package com.timous.web.admin;


import com.timous.Entity.Tag;
import com.timous.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;


    /*按照id的倒叙进行排列*/
    @GetMapping("blogtags")
    public String types(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC)
                                Pageable pageable, Model model) {

        model.addAttribute("page", tagService.listType(pageable));

        return "admin/blogtags";
    }


    //新增标签
    @GetMapping("/tags/taginput")
    public String input() {
        return "admin/taginput";
    }


    @PostMapping("/tags")
    public String post(Tag tag, RedirectAttributes redirectAttributes) {
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1 != null) {
            redirectAttributes.addFlashAttribute("message", "添加失败！！！！已经存在该名称的类型！");
            return "redirect:/admin/blogtags";
        }
        Tag t = tagService.saveType(tag);
        if (t == null) {
            //没保存成功
            redirectAttributes.addFlashAttribute("message", "添加失败！");

        } else {
            //保存成功
            redirectAttributes.addFlashAttribute("message", "添加成功！");
        }
        return "redirect:/admin/blogtags";
    }


    //更新的页面
    @GetMapping("tags/{id}/tagsinput")
    public String editInput(@PathVariable Long id, Model model) {
        model.addAttribute("tag", tagService.getTag(id));
        return "admin/tagupdate";
    }


    //更新操作
    @PostMapping("tags/{id}")
    public String editPost(Tag tag, @PathVariable Long id, RedirectAttributes redirectAttributes) {
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1 != null) {
            redirectAttributes.addFlashAttribute("message", "添加失败！！！！已经存在该名称的类型！");
            return "redirect:/admin/blogtags";
        }

        Tag t = tagService.updateTag(id, tag);


        if (t == null) {
            //没保存成功
            redirectAttributes.addFlashAttribute("message", "修改失败！");

        } else {
            //保存成功
            redirectAttributes.addFlashAttribute("message", "修改成功！");
        }
        return "redirect:/admin/blogtags";


    }

    @GetMapping("tags/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        tagService.deleteTag(id);
        redirectAttributes.addFlashAttribute("message", "删除成功！");
        return "redirect:/admin/blogtags";
    }

}
